package com.stylefeng.guns.modular.tool;

import com.stylefeng.HCNetSDK;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import javax.swing.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

import static com.stylefeng.guns.modular.tool.lianjie.hCNetSDK;

public class Tool {
    public static void delectCar(List list2, List list,List list3,List list4,List list5) {

        for (int w = 0; w <list2.size(); w++) {

            int iErr = 0;

            //设置卡参数
            HCNetSDK.NET_DVR_CARD_CFG_COND m_struCardInputParamSet = new HCNetSDK.NET_DVR_CARD_CFG_COND();
            m_struCardInputParamSet.read();
            m_struCardInputParamSet.dwSize = m_struCardInputParamSet.size();
            m_struCardInputParamSet.dwCardNum = 1;
            m_struCardInputParamSet.byCheckCardNo = 1;

            Pointer lpInBuffer = m_struCardInputParamSet.getPointer();
            m_struCardInputParamSet.write();

            Pointer pUserData = null;
            FRemoteCfgCallBackCardSet fRemoteCfgCallBackCardSet = new FRemoteCfgCallBackCardSet();
            NativeLong IUserID= lianjie.lianjie((String) list2.get(w),(String)list5.get(w),(String)list3.get(w),(String)list4.get(w));
            NativeLong lHandle = hCNetSDK.NET_DVR_StartRemoteConfig(IUserID, HCNetSDK.NET_DVR_SET_CARD_CFG_V50, lpInBuffer, m_struCardInputParamSet.size(), fRemoteCfgCallBackCardSet, pUserData);
            if (lHandle.intValue() < 0) {
                iErr = hCNetSDK.NET_DVR_GetLastError();
                JOptionPane.showMessageDialog(null, "建立长连接失败，错误号：" + iErr);
                return;
            }
            //JOptionPane.showMessageDialog(null, "建立设置卡参数长连接成功!");
            System.out.println("建立设置卡参数长连接成功!");
            HCNetSDK.NET_DVR_FINGER_PRINT_INFO_CTRL_V50 NET_DVR_FINGER_PRINT_INFO_CTRL_V50=new HCNetSDK.NET_DVR_FINGER_PRINT_INFO_CTRL_V50();
            NET_DVR_FINGER_PRINT_INFO_CTRL_V50.read();
            NET_DVR_FINGER_PRINT_INFO_CTRL_V50.byMode=0;
            NET_DVR_FINGER_PRINT_INFO_CTRL_V50.dwSize=NET_DVR_FINGER_PRINT_INFO_CTRL_V50.size();
            NET_DVR_FINGER_PRINT_INFO_CTRL_V50.byRes="0".getBytes();

            HCNetSDK.NET_DVR_CARD_CFG_V50 struCardInfo = new HCNetSDK.NET_DVR_CARD_CFG_V50(); //卡参数

            struCardInfo.read();
            struCardInfo.dwSize = struCardInfo.size();
            struCardInfo.dwModifyParamType = 0x00000001 + 0x00000002 + 0x00000004 + 0x00000008 +
                    0x00000010 + 0x00000020 + 0x00000080 + 0x00000100 + 0x00000200 + 0x00000400 + 0x00000800;
            /***
             * #define CARD_PARAM_CARD_VALID       0x00000001  //卡是否有效参数
             * #define CARD_PARAM_VALID            0x00000002  //有效期参数
             * #define CARD_PARAM_CARD_TYPE        0x00000004  //卡类型参数
             * #define CARD_PARAM_DOOR_RIGHT       0x00000008  //门权限参数
             * #define CARD_PARAM_LEADER_CARD      0x00000010  //首卡参数
             * #define CARD_PARAM_SWIPE_NUM        0x00000020  //最大刷卡次数参数
             * #define CARD_PARAM_GROUP            0x00000040  //所属群组参数
             * #define CARD_PARAM_PASSWORD         0x00000080  //卡密码参数
             * #define CARD_PARAM_RIGHT_PLAN       0x00000100  //卡权限计划参数
             * #define CARD_PARAM_SWIPED_NUM       0x00000200  //已刷卡次数
             * #define CARD_PARAM_EMPLOYEE_NO      0x00000400  //工号
             * #define CARD_PARAM_NAME             0x00000800  //姓名
             */
            String carNo=(String) list.get(0);
            String strCardNo = carNo;
            for (int i = 0; i < HCNetSDK.ACS_CARD_NO_LEN; i++) {
                struCardInfo.byCardNo[i] = 0;
            }
            for (int i = 0; i < strCardNo.length(); i++) {
                struCardInfo.byCardNo[i] = strCardNo.getBytes()[i];
            }


            struCardInfo.byCardValid = 0;
            struCardInfo.byCardType = 1;
            struCardInfo.byLeaderCard = 0;
            struCardInfo.byDoorRight[0] = 1; //门1有权限
            struCardInfo.byDoorRight[1] = 1; //门1有权限
   /*         struCardInfo.byDoorRight[2] = 3; //门1有权限
            struCardInfo.byDoorRight[3] = 4; //门1有权限*/
            struCardInfo.wCardRightPlan[0].wRightPlan[0] = 1; //门1关联卡参数计划模板1
            struCardInfo.wCardRightPlan[1].wRightPlan[1] = 1; //门1关联卡参数计划模板1
/*            struCardInfo.wCardRightPlan[0].wRightPlan[2] = 1; //门1关联卡参数计划模板1
            struCardInfo.wCardRightPlan[0].wRightPlan[3] = 1; //门1关联卡参数计划模板1*/

            //卡有效期
            struCardInfo.struValid.byEnable = 1;
            //int wYear=Integer.parseInt(time[0]);
           // short nwYear= (short)wYear;
           // int byMonth=Integer.parseInt(time[1]);
          //  byte nbyMonths=(byte) byMonth;
           // String[] day = time[2].split(" ");
           // int byDay=Integer.parseInt(day[0]);
           // byte nbyDay=(byte) byDay;
            struCardInfo.struValid.struBeginTime.wYear = 2018;
            struCardInfo.struValid.struBeginTime.byMonth = 8;
            struCardInfo.struValid.struBeginTime.byDay = 9;
            struCardInfo.struValid.struBeginTime.byHour = 0;
            struCardInfo.struValid.struBeginTime.byMinute = 0;
            struCardInfo.struValid.struBeginTime.bySecond = 0;

           // int wYears=Integer.parseInt(stime[0]);
           // short newYear= (short)wYears;
          //  int byMonths=Integer.parseInt(stime[1]);
           // byte nebyMonths=(byte) byMonths;
            //String[] days = stime[2].split(" ");
            //int byDays=Integer.parseInt(days[0]);
           // byte nebyDay=(byte) byDays;
            struCardInfo.struValid.struEndTime.wYear = 2019;
            struCardInfo.struValid.struEndTime.byMonth =8;
            struCardInfo.struValid.struEndTime.byDay = 9;
            struCardInfo.struValid.struEndTime.byHour = 0;
            struCardInfo.struValid.struEndTime.byMinute = 0;
            struCardInfo.struValid.struEndTime.bySecond = 0;

            struCardInfo.dwMaxSwipeTime = 0; //无次数限制
            // struCardInfo.dwSwipeTime = 0;
            struCardInfo.byCardPassword = "12346".getBytes();
            struCardInfo.dwEmployeeNo = 22;

            byte[] strCardName = new byte[0];
            try {
                strCardName = "测试".getBytes("GBK");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            for (int i = 0; i < HCNetSDK.NAME_LEN; i++) {
                struCardInfo.byName[i] = 0;
            }
            for (int i = 0; i < strCardName.length; i++) {
                struCardInfo.byName[i] = strCardName[i];
            }

            struCardInfo.write();
            Pointer pSendBufSet = struCardInfo.getPointer();

            if (!hCNetSDK.NET_DVR_SendRemoteConfig(lHandle, 0x3, pSendBufSet, struCardInfo.size())) {
                iErr = hCNetSDK.NET_DVR_GetLastError();
                JOptionPane.showMessageDialog(null, "ENUM_ACS_SEND_DATA失败，错误号：" + iErr);
                return;
            }

            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            if (!hCNetSDK.NET_DVR_StopRemoteConfig(lHandle)) {
                iErr = hCNetSDK.NET_DVR_GetLastError();
                JOptionPane.showMessageDialog(null, "断开长连接失败，错误号：" + iErr);
                return;
            }

            // JOptionPane.showMessageDialog(null, "断开长连接成功!");
            System.out.println("断开长连接成功!");
        }
    }
}
